import Vue from "vue";
import Router from "vue-router";
import store from "../store/index";

Vue.use(Router);

const router = new Router({
    mode: 'history',
    routes: [{
            path: "/",
            redirect: "/index"
        },
        {
            path: "/index",
            component: () =>
                import ("@/views/index/index"),
            meta: {
                title: "诚德科技",
                // 缓存首页
                keepAlive: true
            }
        },
        {
            path: "/login",
            component: () =>
                import ("@/views/login/index"),
            meta: {
                title: "登陆"
            }
        },
        {
            path: "/photograph",
            component: () =>
                import ("@/views/photograph/index"),
            meta: {
                title: "拍照上传"
            }
        },
        {
            path: "/time",
            component: () =>
                import ("@/views/time/index"),
            meta: {
                title: "计时打卡"
            }
        },
        {
            path: "/repair",
            component: () =>
                import ("@/views/repair/index"),
            meta: {
                title: "维修单"
            }
        },
        {
            path: "/details",
            component: () =>
                import ("@/views/details/index"),
            meta: {
                title: "完成单详情"
            }
        },
        {
            path: "/Submit",
            component: () =>
                import ("@/views/Submit/index"),
            meta: {
                title: "完成提交维修单"
            }
        },
        {
            path: "/ceshi",
            component: () =>
                import ("@/views/ceshi/index"),
            meta: {
                title: "测试"
            }
        },

        // 维保单
        {
            path: "/request",
            component: () =>
                import ("@/views/request/index"),
            meta: {
                title: "维保单"
            }
        },

        {
            path: "/information",
            component: () =>
                import ("@/views/information/index"),
            meta: {
                title: "维修中"
            }
        },
        // 我的
        {
            path: "/my",
            component: () =>
                import ("@/views/my/index"),
            redirect: "/my/center",
            children: [{
                    path: "center",
                    meta: {
                        title: "个人中心"
                    },
                    component: () =>
                        import ("@/views/my/children/center")
                },
                {
                    path: "set",
                    meta: {
                        title: "设置"
                    },
                    component: () =>
                        import ("@/views/my/children/set")
                }
            ]
        },
        {
            path: "*",
            component: () =>
                import ("@/components/NotFound")
        }
    ]
});

router.beforeEach((to, from, next) => {
    let { title, needLogin } = to.meta;
    let { isLogin } = store.state;
    document.title = title;

    if (needLogin && !isLogin) {
        console.log("返回登录");
        next({
            path: "/login"
        });
    } else {
        console.log("进行跳转");
        next();
    }
});

export default router;